jQuery(".bottom").slide({
    titCell: ".hd ul",
    mainCell: ".bd .likeList",
    autoPage: true,
    autoPlay: false,
    effect: "leftLoop",
    autoPlay: true,
    vis: 1
});

function delcomment(id) {
    var split = id.split(",");
    $.get("comment/delComment?id=" + split[0] + "&gid=" + split[1], function (result) {
        if (result == 'ok') {
            $("#comment").load(location.href + " #comment")
        }
    })
}

var gid = 0;
var pid = 0
var goodsBoId = 0
function purchase(id, goodss_id,text,user) {

    if (user==null || user==''){
        if (text=='1'){
            alert("你还没有登录了，不能购买")
        }else{
            alert("你还没有登录了，不能加入购物车")
        }
        return
    }
    if (id!=null && id!=''){
        gid = id;
    }
    if (goodss_id!=null && goodss_id!=''){
        goodsBoId = goodss_id;
    }
    if (goodsBoId==null || goodsBoId==''){
        alert("请选择商品属性")
        return;
    }

    var orderNumber = document.getElementById("number").innerText;
    if (gid!=null && gid!='' && goodsBoId!=null && goodsBoId!='' && text!='' && text!=null){
        $.post("cart/save",{gid,goodsBoId,orderNumber},function (data) {
            if (data!='error'){
                if (text=='1'){
                    window.location.replace("order?id="+data)
                }else{
                    alert("该商品已加入购物车")
                }
            }else if (data=='error'){
                alert("商家没有这么多库存，请重新选择购买数量！！")
            }
        })
    }
}

function upload() {
    document.getElementById("uploadFilexxx").click();
    $(".picDiv").show()
}



function comment(id, comment) {
    $.get("/cart/isPayment?gid=" + id, function (data) {
        if (!data) {
            alert('你没有购买此商品,无法评论')
        }else{
            var file = document.getElementById("uploadFilexxx")
            file.value = ''
            $("#info").val("")
            $("#image").val("")
            if (comment.length > 0) {
                var split = comment.split(",");
                pid = split[0];
                $("#info").val('@' + split[1] + ' ')
                $("#uploadBut").hide()
                $(".picDiv").hide()
            } else {
                $("#uploadBut").show()
            }
            gid = id;
            $(".comment").show()
            $(".mask").show()
        }
    })

}

$(".bc>input").click(function () {
    if ($(this).val() == "保存") {
        if ($("#info").val().length > 0) {
            $(".mask").hide();
            $(".comment").hide()
        }
    } else {
        pid = 0;
        $(".mask").hide();
        $(".comment").hide()
    }
});
//图片上传预览功能
var userAgent = navigator.userAgent; //用于判断浏览器类型
//使用formData完成多图片上传
var formData = new FormData();
var formData2 = new FormData();
$(".file").change(function () {
    //获取选择图片的对象
    var docObj = $(this)[0];
    var picDiv = $(this).parents(".picDiv");
    //得到所有的图片文件
    var fileList = docObj.files;
    //将图片数据放到formData中传递后台
    if ($(this)[0].files.length == 1) {
        formData.append("file", docObj.files[0]);
        formData2.append("file", docObj.files[0]);
    } else {
        for (var i = 0; i < $(this)[0].files.length; i++) {
            formData.append("file", $(this)[0].files[i]);
            formData2.append("file", $(this)[0].files[i]);
        }
    }
    //循环遍历
    for (var i = 0; i < fileList.length; i++) {
        //动态添加html元素
        var picHtml = "<div class='imageDiv' nm='" + fileList[i].name + "'> <img id='img" + fileList[i].name + "' /> <div class='cover'><i class='delbtn'>删除</i></div></div>";
        console.log(picHtml);
        picDiv.prepend(picHtml);
        //获取图片imgi的对象
        var imgObjPreview = document.getElementById("img" + fileList[i].name);
        if (fileList && fileList[i]) {
            //图片属性
            imgObjPreview.style.display = 'block';
            imgObjPreview.style.width = '160px';
            imgObjPreview.style.height = '130px';
            //imgObjPreview.src = docObj.files[0].getAsDataURL();
            //火狐7以上版本不能用上面的getAsDataURL()方式获取，需要以下方式
            if (userAgent.indexOf('MSIE') == -1) {
                //IE以外浏览器
                imgObjPreview.src = window.URL.createObjectURL(docObj.files[i]); //获取上传图片文件的物理路径;
                console.log(imgObjPreview.src);
                // var msgHtml = '<input type="file" id="fileInput" multiple/>';
            } else {
                //IE浏览器
                if (docObj.value.indexOf(",") != -1) {
                    var srcArr = docObj.value.split(",");
                    imgObjPreview.src = srcArr[i];
                } else {
                    imgObjPreview.src = docObj.value;
                }
            }
        }
    }
    /*删除功能*/
    $(".delbtn").click(function () {
        if (formData.getAll("file").length == 0) {
            formData = formData2;
        } else {
            formData2.delete("file");
            for (var i = 0; i < formData.getAll("file").length; i++) {
                if (formData.getAll("file")[i].name == $(this).parents(".imageDiv").attr("nm")) {

                } else {
                    formData2.append("file", formData.getAll("file")[i]);
                }
            }
        }
        formData.delete("file");
        for (var i = 0; i < formData2.getAll("file").length; i++) {
            formData.append("file", formData2.getAll("file")[i]);
        }
        var _this = $(this);
        _this.parents(".imageDiv").remove();
    });
});

function commentSum() {
    if ($("#info").val().length > 0) {
        console.log(formData2.getAll("file").length)
        if (formData2.getAll("file").length > 0) {
            $.ajax({
                type: 'post',
                dataType: 'json',
                data: formData2,
                cache: false, //上传文件不需要缓存
                url: '/upload',
                processData: false, // 告诉jQuery不要去处理发送的数据
                contentType: false, // 告诉jQuery不要去设置Content-Type请求头
                success: function (data) {
                    if (data.success == 'ok') {
                        $("#image").val(data.imags)
                        var info = $("#info").val()
                        var images = $("#image").val();
                        $.ajax({
                            type: 'post',
                            data: {info, images, gid, pid},
                            url: 'comment/save',
                            success: function (data) {
                                pid =0;
                                $("#comment").load(location.href + " #comment")
                            }
                        })
                    }
                }
            })

        } else {
            var info = $("#info").val()
            var images = $("#image").val()
            $.ajax({
                type: 'post',
                data: {info, images, gid, pid},
                url: 'comment/save',
                success: function (data) {
                    pid =0;
                    $("#comment").load(location.href + " #comment")
                }
            })
        }
    }


}